package com.asia5b.a5mall.middleware.logic.viewmodel.shop

import androidx.lifecycle.MutableLiveData
import com.asia5b.a5mall.middleware.base.SimpleViewModel
import com.asia5b.a5mall.middleware.model.shop.AbsGoodsModel
import com.asia5b.a5mall.middleware.utils.removeAllOffGoods
import com.google.gson.reflect.TypeToken
import io.reactivex.Observable
import io.reactivex.functions.Consumer
import io.reactivex.functions.Function3

class ShopHomeFragmentVM :SimpleViewModel() {


    /**
     * @Note   加载首页数据
     * @param  shopId 店铺ID
     */
    fun getFirstGoodsDatas(shopId: Int) {
        val hotObservable = goodsApi.getTagGoodsList("hot", shopId, 20)
        val newObservable = goodsApi.getTagGoodsList("new", shopId, 20)
        val recommendObservable = goodsApi.getTagGoodsList("recommend", shopId, 20)
        val observable = Observable.zip(hotObservable, newObservable, recommendObservable
                , Function3<ArrayList<AbsGoodsModel>, ArrayList<AbsGoodsModel>, ArrayList<AbsGoodsModel>, ArrayList<AbsGoodsModel>> { t1, t2, t3 ->
            val type = object : TypeToken<ArrayList<AbsGoodsModel>>() {}.type
            val list = arrayListOf<AbsGoodsModel>()
            if (t1.isNotEmpty()) {
                //添加头部
                list.add(AbsGoodsModel(AbsGoodsModel.hot))
                //添加数据
                list.addAll(t1)
            }
            if (t2.isNotEmpty()) {
                //添加头部
                list.add(AbsGoodsModel(AbsGoodsModel.new))
                //添加数据
                list.addAll(t2)
            }
            if (t3.isNotEmpty()) {
                //添加头部
                list.add(AbsGoodsModel(AbsGoodsModel.recommend))
                //添加数据
                list.addAll(t3)
            }
            return@Function3 list
        })
        baseSubscribe(observable, Consumer { data ->
//            providerView().getFirstGoodsDatasResult(removeAllOffGoods(data))
            getFirstGoodsDatasResultLiveData.value=removeAllOffGoods(data)
        })
    }
    val getFirstGoodsDatasResultLiveData= MutableLiveData<ArrayList<AbsGoodsModel>>()
}